package com.example.phpclient.fragment;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import androidx.fragment.app.Fragment;

import com.example.phpclient.DB.NotesDB;
import com.example.phpclient.R;
import com.example.phpclient.content.AddContent;
import com.example.phpclient.content.MyAdapter;
import com.example.phpclient.content.SelectAct;


public class Manager_three_Fragment1 extends Fragment {
//    private Button textbtn,imgbtn,videobtn;
    private ListView lv;
    private Intent intent;//为了按钮点击事件的传值
    private Cursor cursor;

    private MyAdapter adapter;
    private NotesDB notesDB;
    private SQLiteDatabase dbReader;
    View view;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState){
        view = inflater.inflate(R.layout.note_activity,container,false);
        initView();
        return view;
    }
    public void initView(){
        lv =(ListView) view.findViewById(R.id.note_list);
//        textbtn =(Button) view.findViewById(R.id.addtext);
//        imgbtn =(Button) view.findViewById(R.id.addimg);
//        videobtn =(Button) view.findViewById(R.id.addvideo);






//        textbtn.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                intent = new Intent(getActivity(), AddContent.class);
//                intent.putExtra("flag","1");
//                startActivity(intent);
//            }
//        });
//        imgbtn.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                intent = new Intent(getActivity(), AddContent.class);
//                intent.putExtra("flag","2");
//                startActivity(intent);
//            }
//        });
//        videobtn.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                intent = new Intent(getActivity(), AddContent.class);
//                intent.putExtra("flag","3");
//                startActivity(intent);
//
//            }
//        });

        notesDB =new NotesDB(getActivity());
        dbReader = notesDB.getWritableDatabase();

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                cursor.moveToPosition(i);
                Intent intent = new Intent(getActivity(), SelectAct.class);
                intent.putExtra(NotesDB.ID,cursor.getInt(cursor.getColumnIndex(NotesDB.ID)));
                intent.putExtra(NotesDB.CONTENT,cursor.getString(cursor.getColumnIndex(NotesDB.CONTENT)));
                intent.putExtra(NotesDB.TIME,cursor.getString(cursor.getColumnIndex(NotesDB.TIME)));
                intent.putExtra(NotesDB.PATH,cursor.getString(cursor.getColumnIndex(NotesDB.PATH)));
                intent.putExtra(NotesDB.VIDEO,cursor.getString(cursor.getColumnIndex(NotesDB.VIDEO)));
                startActivity(intent);
            }
        });


    }
    @Override
    public void onResume() {
        super.onResume();
        selectDB();
    }

    public void selectDB(){
        cursor =dbReader.query(NotesDB.TABLE_NAME,null,null,null,null,null,null);
        adapter = new MyAdapter(getActivity(),cursor);
        lv.setAdapter(adapter);
    }

}
